<template>

  <div>
    <el-form ref="form"  label-width="80px" inline>
      <el-form-item label="房间名称">
        <el-input v-model="queryParam.name" placeholder="请输房间名称" ></el-input>
      </el-form-item>
      <el-form-item label="房间号">
        <el-input v-model="queryParam.roomNumber" placeholder="请输房间号" ></el-input>
      </el-form-item>
      <el-form-item  >
        <el-button type="primary" size="mini" @click="search()">搜索</el-button>
      </el-form-item>
    </el-form>


    <el-button type="primary" @click="dialogFormVisible=true" >添加</el-button>




    <el-table :data="tableData" size="mini" style="width: 100%" v-loading="loading">
      <el-table-column prop="roomsId" label="房间id" align="center" width="180"></el-table-column>
      <el-table-column prop="roomNumber" label="房间号" align="center"></el-table-column>
      <el-table-column prop="name" label="房间名称" align="center"></el-table-column>
      <el-table-column prop="floor" label="楼层" align="center"></el-table-column>
      <el-table-column prop="area" label="房间面积" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">

          <el-button type="primary" size="mini" @click="toEdit(scope.row.roomsId)">修改</el-button>
          <el-button type="warning" size="mini" @click="deleteById(scope.row.roomsId)">删除</el-button>
        </template>

      </el-table-column>

    </el-table>

    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
          @size-change="queryList"
          @current-change="queryList"
          :current-page.sync="queryParam.current"
          :page-size.sync="queryParam.size"
          :page-sizes="[5,10,15,20,50]"
          layout="total,sizes, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog title="添加房间" :visible.sync="dialogFormVisible" v-if="dialogFormVisible">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="房间号">
          <el-input v-model="form.roomNumber" ></el-input>
        </el-form-item>
        <el-form-item label="房间名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="楼层">
          <el-input v-model="form.floor"></el-input>
        </el-form-item>
        <el-form-item label="房间面积">
          <el-input v-model="form.area"></el-input>
        </el-form-item>

        <el-form-item label="">
          <el-button type="primary" @click="save()">保存</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>

  </div>


</template>

<script>


// import axios from "axios";
// axios.defaults.baseURL = "http://localhost:8080";
import service from "@/utils/request.js";

export default {
  name: 'Dept',

  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      loading: false,
      form:{
        roomsId:'',
        name:'',
        floor:'',
        roomNumber:'',
        area:''
      },
      total:0,
      pages:0,
      queryParam:{
        current:1,
        size:5,
        name:'',
        roomNumber:''
      }
    }
  },
  methods: {
    search(){
      this.queryParam.current=1;
      this.queryList();
    },
    queryList() {
      this.loading = true;
      service.get("/rooms/page",{params:this.queryParam}).then((res) => {
        this.tableData = res.records;
        this.total = res.total;
        console.log("tableData:" ,this.tableData)
        console.log("total:" ,this.total)
        this.pages = res.pages;
        this.loading = false
      })
    },
    toEdit(id){
      service.get("/rooms/getById/"+id).then((res) => {
        this.form = res;
        this.dialogFormVisible = true;
      })
    },
    deleteById(id){
      this.$confirm('是否删除', '确认信息') .then(() => {
        service.get("/rooms/removeById/"+id).then((res) => {
          const success = res
          if(success){
            this.$message.success("删除成功");
            this.queryList();
          }
        })
      });




    },
    save(){
      service.post("/rooms/saveOrUpdate",this.form  ).then((res) => {
        const success = res
        if(success){
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.queryList();
        }
      })
    }
  },
  created() {
    this.queryList()

  }

}
</script>

<style scoped>


</style>